﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace com.sharepointutils.application
{
    public static class StringExtension
    {
        const String SHAREPOINT_SAFE_EXPRESSION = @"[#%&,;""\*:\<\>\?\\/{|}~]";

        /// <summary>
        /// Removes all invalid charaters for SharePoint.
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static String MakeSafe(this String value)
        {
            return Regex.Replace(value, SHAREPOINT_SAFE_EXPRESSION, "");
        }

        /// <summary>
        /// Returns two Strings in a array seperated by the value supplied at the first occurances.
        /// </summary>
        /// <param name="value">Input String</param>
        /// <param name="seperator">String to seperate the input string</param>
        /// <returns></returns>
        public static String[] Halve(this String value, String seperator)
        {
            String[] ret = { "", "" };
            int index = 0;
            while (index == 0)
            {
                index = value.IndexOf(seperator);
                if (index == 0)
                {
                    value = value.Remove(index, 1);
                }
            }
            index = (index == -1) ? value.Length - 1 : index;
            ret[0] = value.Substring(0, index);
            if ((index + 1) < (value.Length - 1))
            {
                ret[1] = value.Substring(index + 1);
            }
            return ret;
        }


    }
}
